package com.yuke.cloud.service.smc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.DateUtil;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.omc.dto.DmsStoreOrderApi;
import com.yuke.cloud.service.api.omc.dto.OdsOrderApiDto;
import com.yuke.cloud.service.api.omc.service.OmcOrderFeignApi;
import com.yuke.cloud.service.smc.dao.DmsStoreMapper;
import com.yuke.cloud.service.smc.dto.DmsStoreDto;
import com.yuke.cloud.service.smc.dto.DmsVisitDto;
import com.yuke.cloud.service.smc.dto.SaleRankDto;
import com.yuke.cloud.service.smc.entity.DmsScope;
import com.yuke.cloud.service.smc.entity.DmsStore;
import com.yuke.cloud.service.smc.entity.DmsVisit;
import com.yuke.cloud.service.smc.service.DmsStoreService;
import com.yuke.cloud.service.smc.service.DmsVisitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* Created by zn on 2020/07/16.
*/
@Api(value = "WEB - DmsVisitController", tags = "处理DmsVisit相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/dms/visit")
public class DmsVisitController extends BaseController {
    @Resource
    private DmsVisitService dmsVisitService;

    @Resource
    private DmsStoreMapper dmsStoreMapper;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "dmsVisit", value = "该数据记录的POJO") @RequestBody DmsVisit dmsVisit) {
        System.out.println(dmsVisit);
        dmsVisitService.save(dmsVisit);


        if (dmsVisit.getVisitStatus() != null && dmsVisit.getVisitStatus().equals((byte)1)){

            DmsStore dmsStore = new DmsStore();
            dmsStore.setStoreId(dmsVisit.getStoreId());
            dmsStore.setStoreStatus((byte)1);
            dmsStoreMapper.updateByPrimaryKeySelective(dmsStore);
        }

        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        dmsVisitService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "dmsVisit", value = "需要修改的内容") @RequestBody DmsVisit dmsVisit) {
        dmsVisitService.update(dmsVisit);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<DmsVisit> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        DmsVisit dmsVisit = dmsVisitService.findById(id);
        return WrapMapper.ok(dmsVisit);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<DmsVisit>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<DmsVisit> list = dmsVisitService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }



    /**
     * 根据条件返回相应的值
     * @param page
     * @param size
     * @param dmsVisitDto
     * @return
     */
    @RequestMapping(value = "/countVisit",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "分页按条件获取所有记录")
    public Wrapper<PageInfo<DmsVisitDto>> getList(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                  @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                  @ApiParam(name = "dmsVisitDto",value = "拜访结果") @RequestBody DmsVisitDto dmsVisitDto)
    {
        PageHelper.startPage(page, size);
        List<DmsVisitDto> dmsVisitDtos = dmsVisitService.selectVisitByCondition(dmsVisitDto);
        PageInfo pageInfo = new PageInfo(dmsVisitDtos);
        System.out.println(dmsVisitDto);
        return WrapMapper.ok(pageInfo);
    }

    //根据业务员id查询所有合作店铺的本月下单金额和下单次数
    @RequestMapping(value = "/countAmount",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "根据业务员id查询所有合作店铺的本月下单金额和下单次数")
    public Wrapper<PageInfo<OdsOrderApiDto>> getAmount(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {

        PageInfo<OdsOrderApiDto> list = dmsVisitService.getAmountById(userId, page, size);

        System.out.println(userId);

        return WrapMapper.ok(list);
    }

    //根据storeId更新shopId
    @RequestMapping(value = "/updateShopId",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "根据storeId更新shopId")
    public void checkShopId( @RequestBody DmsVisitDto dmsVisitDto)
    {
        dmsVisitService.checkShopId(dmsVisitDto.getStoreId());
        System.out.println(dmsVisitDto);
    }

    /**(业务员需要回访的店铺
     1.一周内没有回访的
     2.下单金额变少的店铺
     */
    //@RequestMapping(value = "/needVisitShop",method = RequestMethod.POST)
    @RequestMapping(value = "/needVisitShop",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "一周内没有回访的")
    public Wrapper<PageInfo<DmsStoreDto>> needVisitShop(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {
        int num=7;
        PageHelper.startPage(page, size);
        List<DmsStoreDto> list = dmsVisitService.needVisitShop(userId,num);
        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    /*业务员需要回访的店铺
    1.一周内没有回访的
    2.下单金额较少的
     */
    @RequestMapping(value = "/visitInactiveStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "下单金额较少的")
    public Wrapper<PageInfo<DmsStoreDto>> visitInactiveStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {
        PageHelper.startPage(page, size);
        List<DmsStoreDto> list = dmsVisitService.visitInactiveStore(userId);
        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    /**业务员跟进的店铺
     */
    @RequestMapping(value = "/needDevShop",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员正在跟进的店铺")
    public Wrapper<PageInfo<DmsStoreDto>> needDevShop(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {
        PageHelper.startPage(page, size);
        List<DmsStoreDto> list = dmsVisitService.needDevStore(userId);
        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    //本月 业务员已经访问过/打卡过的信息 未去重
    @RequestMapping(value = "/visitedStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "本月 业务员已经访问过/打卡过的信息 未去重")
    //public List<DmsVisitDto> visitedStore(@RequestParam Long userId, @RequestParam Date beginTime, @RequestParam Date endTime)
    public Wrapper<PageInfo<DmsVisitDto>> visitedStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {
        PageHelper.startPage(page, size);
        List<DmsVisitDto> list = dmsVisitService.visitedStore(userId);
        PageInfo<DmsVisitDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    //业务员已经访问过/打卡过的店铺所有 去重
    @RequestMapping(value = "/visitedAllStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员已经访问过/打卡过的店铺所有")
    public  Wrapper<PageInfo<DmsStoreDto>> visitedAllStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {
        PageHelper.startPage(page, size);
        List<DmsStoreDto> list = dmsVisitService.visitedAllStore(userId);
        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    /*业务员 我的 统计

     */
    @RequestMapping(value = "/userCount",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员 我的 统计")
    public  Wrapper<Map<String,Integer>> userCount(@ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId) {

        Map<String,Integer> list = dmsVisitService.userCount(userId);

        return WrapMapper.ok(list);
    }

    /*业务员今日已走访
     * */
    @RequestMapping(value = "/visitedStoreByDay",method = RequestMethod.POST)
    //public List<DmsVisitDto> visitedStore(@RequestParam Long userId, @RequestParam Date beginTime, @RequestParam Date endTime)
    @ApiOperation(httpMethod = "POST", value = "业务员今日已走访")
    public Wrapper<PageInfo<DmsVisitDto>> visitedStoreByDay(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {
        PageHelper.startPage(page, size);
        List<DmsVisitDto> list = dmsVisitService.visitedStoreByDay(userId);
        PageInfo<DmsVisitDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);

    }

    /*
    业务员今日统计
     */
    @RequestMapping(value = "/homeCount",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员 我的 统计")
    public  Wrapper<Map<String,Integer>> homeCount(@ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId) {

        Map<String,Integer> list = dmsVisitService.homeCount(userId);

        return WrapMapper.ok(list);
    }

    //业务员排行榜
    //业务员已经访问过/打卡过的店铺所有 去重
    @RequestMapping(value = "/sellRanking",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员已经访问过/打卡过的店铺所有")
    public Wrapper<List<SaleRankDto>> sellRanking(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size
    )
    {
        //PageHelper.startPage(page, size);
        List<SaleRankDto> list = dmsVisitService.sellRanking();
        //PageInfo<Map<Long,Long>> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(list);
    }


    /*
    业务员待服务
     */
    @RequestMapping(value = "/needServiceShop",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "业务员待服务")
    public Wrapper<PageInfo<DmsStoreDto>> needServiceShop(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size
    )
    {
        PageHelper.startPage(page, size);
        List<DmsStoreDto> list = dmsVisitService.needServiceShop();
        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    /*
   业务员待跟进
    */
    @RequestMapping(value = "/publicDevStore",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "待跟进")
    public Wrapper<PageInfo<DmsStoreDto>> publicDevStore(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size)
    {
        PageHelper.startPage(page, size);
        List<DmsStoreDto> list = dmsVisitService.publicDevStore();
        PageInfo<DmsStoreDto> pageInfo = new PageInfo<>(list);
        return WrapMapper.ok(pageInfo);
    }

    //根据业务员id查询所有合作店铺的本月下单金额和下单次数 根据storeId去查shopId,多个shopId可能对应一个storeId
    @RequestMapping(value = "/countShopByStoreId",method = RequestMethod.POST)
    @ApiOperation(httpMethod = "POST", value = "根据业务员id查询所有合作店铺的本月下单金额和下单次数")
    public Wrapper<PageInfo<OdsOrderApiDto>> countShopByStoreId(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "userId", value = "用户Id") @RequestParam Long userId)
    {

        PageInfo<OdsOrderApiDto> list = dmsVisitService.countShopByStoreId(userId, page, size);

        System.out.println(userId);

        return WrapMapper.ok(list);
    }



}
